/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.library.Constants;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;

import java.io.IOException;

/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public abstract class BaseMaterialFragment extends BaseFragment {

    private Component mHeaderBox;
    private Component mHeaderContentBox;
    private Component mHeaderBackgroundBox;
    private Component mHeaderShadow;

    Text mTitleHeader;
    Text mSubTitleHeader;

    Image mSourceButton;

    private int colorResId = -1;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setupBarHeader();
        setupBarButton();

        try {
            getWindow().setBackgroundColor(new RgbColor(getResourceManager().getElement(ResourceTable.Color_native_background).getColor()));
        } catch (Exception e) {
             HiLog.error(Constants.LABEL_LOG,e.toString());
        }


//        float mMaxHeaderElevation = getResources().getDimensionPixelSize(
//                ResourceTable.Float_carddemo_barheader_elevation);

        if (mHeaderShadow != null)
            mHeaderShadow.setVisibility(Component.HIDE);
//        if (mHeaderBackgroundBox != null)
//            lpu.setViewElevation(mHeaderBackgroundBox, mMaxHeaderElevation);
//        if (mHeaderContentBox != null)
//            lpu.setViewElevation(mHeaderContentBox, mMaxHeaderElevation + 0.1f);

        if (mTitleHeader != null)
            mTitleHeader.setText(getString(getTitleHeaderResourceId()));

        if (mSubTitleHeader != null)
            mSubTitleHeader.setText(getString(getSubTitleHeaderResourceId()));


        if (colorResId != -1) {
            // make sure it's opaque
            //((mSessionColor = UIUtils.setColorAlpha(mSessionColor, 255);
            try {
                ShapeElement shapeElement = new ShapeElement();
                RgbColor rgbColor = new RgbColor(getResourceManager().getElement(colorResId).getColor());
                shapeElement.setRgbColor(rgbColor);
                mHeaderBackgroundBox.setBackground(shapeElement);
            } catch (Exception e) {
                 HiLog.error(Constants.LABEL_LOG,e.toString());
            }
        }
    }


    protected void setupBarHeader() {
        mHeaderBox = findComponentById(ResourceTable.Id_header_recap);
        mHeaderContentBox = findComponentById(ResourceTable.Id_header_contents);
        mHeaderBackgroundBox = findComponentById(ResourceTable.Id_header_background);
        mHeaderShadow = findComponentById(ResourceTable.Id_header_shadow);

        mTitleHeader = (Text) findComponentById(ResourceTable.Id_header_title);
        mSubTitleHeader = (Text) findComponentById(ResourceTable.Id_header_subtitle);
    }

    protected void setupBarButton() {
        mSourceButton = (Image) findComponentById(ResourceTable.Id_bar_button_source);

        final String sourceUrl = getSourceUrl();
        final String docUrl = getDocUrl();

        if (mSourceButton != null) {


            if (sourceUrl == null) {
                mSourceButton.setEnabled(false);
            } else {

                mSourceButton.setClickedListener(view -> {
                    //TODO
//                    Intent i = new Intent(Intent.ACTION_VIEW);
//                    i.setData(Uri.parse(sourceUrl));
//                    startActivity(i);
                });
            }
        }

    }


    public static int scaleColor(int color, float factor, boolean scaleAlpha) {
        return Color.argb(scaleAlpha ? (Math.round(Color.alpha(color) * factor)) : Color.alpha(color),
                Math.round(new RgbColor(color).getRed() * factor), Math.round(new RgbColor(color).getGreen() * factor),
                Math.round(new RgbColor(color).getBlue() * factor));
    }


    protected abstract int getSubTitleHeaderResourceId();

    protected abstract int getTitleHeaderResourceId();

    protected abstract String getDocUrl();

    protected abstract String getSourceUrl();
}
